#!/usr/bin/env python
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from model import CourseM
from model import LectureM
from model import GeneralM

class coursePage(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            key = self.request.get('key')
            if key:
                viewVals = GeneralM.getViewVals()
                course = CourseM.getCourseByKey(key);
                viewVals['course'] = CourseM.course2Dict(course)
                viewVals['lectures'] = map(LectureM.lecture2DictWith, LectureM.getLecturesByCourse(course))
                if len(viewVals['lectures']) == 0:
                    viewVals['lectures'] = None
                self.response.out.write(template.render('view/course_stats.djhtml', viewVals))
            else:
                self.redirect("/allCourses")
        else:
            self.redirect("/login")

application = webapp.WSGIApplication([(r'.*', coursePage)],debug=True)

def main():
    run_wsgi_app(application)
    
if __name__ == '__main__':
    main()